(:
  A procedure to select the contents of the nth worksheet in excel 
  - first row indicates property names
  - time, begin and end are reserved property names
  - include date creates a date field.
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
    
declare namespace xcl = "urn:schemas-microsoft-com:office:spreadsheet";

declare function local:get-timestamp($datecell as node()) as xs:integer
{
let $date := xs:dateTime(string($datecell))
return jj:date-to-millis($date)
};

<display>{
let $t := $arguments[1]
let $worksheetn := xs:integer($arguments[2])
(:let $includedate := if($arguments[3]) then $arguments[3]=("Yes","yes") else fn:false():)
let $worksheet := doc($t)/xcl:Workbook/xcl:Worksheet[position()=$worksheetn]
let $cols := for $cell in $worksheet/xcl:Table/xcl:Row[1]/xcl:Cell where string($cell)!="" return string($cell)
let $begincol := 1
let $endcol := 2
		
for $row at $p in $worksheet/xcl:Table/xcl:Row[position()>1]
    let $time := local:get-timestamp($row/xcl:Cell[$begincol])
    let $duration := local:get-timestamp($row/xcl:Cell[$endcol]) - $time
		let $millis := $time 
		order by $time
		return
		 <item>{
		    <info name="num">{$p}</info>,
    		<info name="time"><time><date>{$millis}</date><duration>{$duration}</duration></time></info>,
    		<info name="src-anchor">{
      		<anchor>{
        		<doc>{ $t }</doc>,
        		<path>{jj:build-Path($row)}</path>
      	  }</anchor>
    		}</info>,
    		(for $col at $colp in $cols  
    		let $s := string($row/xcl:Cell[$colp])
    		let $nonnullstring := if ($s) then $s else "_"
    		where fn:not($col = ("begin","end","time"))
    		return <info name="{$col}">{$nonnullstring}</info>)
    }</item>
}</display>
